package com.kly.cashmall.module.home.home_tab.tab_container;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;

import com.kly.cashmall.R;
import com.kly.cashmall.utils.data.DensityUtil;


/**
 * @author
 * @date 2017/10/23
 */
public class TopScrollTabContainerView extends LinearLayout implements TabContainerView {

  private static final int SCROLL_SIZE = 4;
  private LinearLayout tabContainer;
  private FrameLayout fragmentFrame;
  private OnItemClickListener itemClickListener;

  public TopScrollTabContainerView(Context context) {
    this(context, null);
  }

  public TopScrollTabContainerView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public TopScrollTabContainerView(Context context, @Nullable AttributeSet attrs,
                                   int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
  }

  private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    setContentView(R.layout.layout_tab_top_scroll_tab_container_view);
  }

  public void setContentView(@LayoutRes int layoutId) {
    LayoutInflater.from(getContext()).inflate(layoutId, this, true);
    fragmentFrame = (FrameLayout) findViewById(R.id.tab_fragment_container);
    tabContainer = (LinearLayout) findViewById(R.id.tab_container);
  }

  @Override
  public int getFragmentContainerId() {
    return fragmentFrame.getId();
  }

  @Override
  public void addTabChild(View tabChild) {

    LayoutParams params =
        new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    tabContainer.addView(tabChild, params);

    tabChild.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (itemClickListener != null) {
          if(itemClickListener.onItemClick(tabContainer.indexOfChild(v), v)){
            setSelectedItem(tabContainer.indexOfChild(v));
          }
        }
      }
    });

    adjustItemSize();
  }

  /**
   * 超过4个的时候调整Item可以滑动
   */
  private void adjustItemSize() {
    int minPadding = DensityUtil.dip2px(getContext(), 20);
    int width = getResources().getDisplayMetrics().widthPixels;

    for (int i = 0; i < getSize(); i++) {
      tabContainer.getChildAt(i).setMinimumWidth(width / Math.min(SCROLL_SIZE, getSize()));
      tabContainer.getChildAt(i).setPadding(minPadding, 0, minPadding, 0);
    }
  }

  @Override
  public int getSize() {
    return tabContainer.getChildCount();
  }

  @Override
  public void removeTabContainer() {
    tabContainer.removeAllViews();
  }

  /**
   * 选中项
   */
  @Override
  public void setSelectedItem(int pos) {
    for (int i = 0; i < tabContainer.getChildCount(); i++) {
      View view = tabContainer.getChildAt(i);
      if (view != null) {
        view.setEnabled(pos != i);
        view.setSelected(pos == i);
      }
    }
  }

  @Override
  public void setItemClickListener(OnItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
  }
}
